<?php $title = "首页轮播图"; ?>
<!DOCTYPE html>
<html>

<head>
    <?php include('css.php'); ?>
</head>

<body>

    <?php include('nav.php'); ?>
    <div class="main-content">
        <div class="container-fluid">
            <div class="row mb-4">
                <div class="col">
                    <h1 class="page-title"><?php echo $title; ?></h1>
                    <div class="mt-3">
                        <a href="banner.php?menu=upload" class="btn btn-primary">上传新的首页轮播图</a>
                        <a href="banner.php?menu=manager" class="btn btn-primary">管理首页轮播图</a>
                    </div>
                    <div class="container mt-3 mb-3">
                        <?php
                        if (isset($_GET['menu'])) {
                            $menu = $_GET['menu'];
                            switch ($menu) {
                                case 'upload':
                        ?>
                                    <div class="mt-3">
                                        <form action="toset.php?action=banner_upload" method="post" enctype="multipart/form-data">
                                            <input type="hidden" name="action" value="upload">
                                            <div class="mb-3">
                                                <label class="form-label" for="title">标题</label>
                                                <input type="text" name="title" class="form-control" id="title" required>
                                            </div>
                                            <div class="mb-3">
                                                <label class="form-label" for="slogan">口号</label>
                                                <input type="text" name="slogan" class="form-control" id="slogan" required>
                                            </div>
                                            <div class="mb-3">
                                                <label class="form-label" for="banner_image">Banner图片</label>
                                                <input type="file" name="banner_image" class="form-control" id="banner_image" accept="image/*" required>
                                            </div>
                                            <button type="submit" class="btn btn-primary">上传</button>
                                        </form>
                                    </div>
                                <?php break;

                                case 'manager':
                                    $banners = mysqli_query($link, "SELECT * FROM web_banners ORDER BY id DESC");
                                ?>
                                    <div class="container mt-3">

                                        <div class="row">
                                            <?php while ($banner = mysqli_fetch_assoc($banners)): ?>
                                                <div class="col-md-6 mb-4">
                                                    <div class="card">
                                                        <div class="card-body">
                                                            <div class="hero-section" style="
                            height: 200px;
                            background-image: url('<?= $banner['url'] ?>');
                            background-size: cover;
                            background-position: center;
                            margin-bottom: 15px;
                        "></div>
                                                            <h5><?= htmlspecialchars($banner['title']) ?></h5>
                                                            <p><?= htmlspecialchars($banner['slogan']) ?></p>

                                                            <form action="toset.php" method="post" class="d-inline">
                                                                <input type="hidden" name="action" value="update">
                                                                <input type="hidden" name="id" value="<?= $banner['id'] ?>">
                                                                <button type="button" class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editModal<?= $banner['id'] ?>">
                                                                    编辑
                                                                </button>
                                                            </form>

                                                            <a href="toset.php?action=banner_delete&id=<?= $banner['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('确定删除吗？')">删除</a>

                                                            <!-- 编辑模态框 -->
                                                            <div class="modal fade" id="editModal<?= $banner['id'] ?>" tabindex="-1">
                                                                <div class="modal-dialog">
                                                                    <div class="modal-content">
                                                                        <form action="toset.php?action=banner_update" method="post">
                                                                            <input type="hidden" name="action" value="update">
                                                                            <input type="hidden" name="id" value="<?= $banner['id'] ?>">
                                                                            <div class="modal-header">
                                                                                <h5 class="modal-title">编辑Banner</h5>
                                                                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                                            </div>
                                                                            <div class="modal-body">
                                                                                <div class="mb-3">
                                                                                    <label class="form-label">标题</label>
                                                                                    <input type="text" name="title" class="form-control" value="<?= htmlspecialchars($banner['title']) ?>" required>
                                                                                </div>
                                                                                <div class="mb-3">
                                                                                    <label class="form-label">口号</label>
                                                                                    <input type="text" name="slogan" class="form-control" value="<?= htmlspecialchars($banner['slogan']) ?>" required>
                                                                                </div>
                                                                            </div>
                                                                            <div class="modal-footer">
                                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                                                                <button type="submit" class="btn btn-primary">保存</button>
                                                                            </div>
                                                                        </form>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php endwhile; ?>
                                        </div>
                                    </div>
                        <?php break;
                            }
                        } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php include('bottom.php'); ?>
</body>

</html>